const { add, find, update, del, addAll } = require("../../server");
const message = require("../../model/message");
const message1 = require("../../model/message1");

//查询全部数据
const getCtr = async (req, res, next) => {
  try {
    let data = await find(message);
    res.send({
      code: 200,
      data,
    });
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

//新增
const postCtr = async (req, res, next) => {
  let data = req.body;
  console.log(data);
  try {
    await add(message, data);
    res.send({
      code: 200,
      message: "数据添加成功",
    });
  } catch (error) {
    res.send({
      code: 400,
      message: "数据添加失败",
    });
  }
};

// 文件导入
const postCtr1 = async (req, res, next) => {
  let data = req.body;
  try {
    await addAll(message, data);
    res.send({
      code: 200,
      message: "数据插入成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "数据插入失败",
    });
  }
};

// 信息修改
const putCtr = async (req, res, next) => {
  let data = req.body;
  try {
    let bob = await update(
      message,
      {
        type: data.type,
        content: data.content,
        createdTime: data.createdTime,
        status: data.status,
        nickname: data.title,
      },
      {
        id: data.id,
      }
    );
    if (bob == 0) {
      res.send({
        code: 400,
        message: "未进行修改操作",
      });
      return;
    }
    res.send({
      code: 200,
      message: "修改成功!",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "抱歉,修改失败!",
    });
  }
};

//模糊查询
const getCtr1 = async (req, res, next) => {
  let tiao = req.query;
  try {
    let data = await find(message, {
      nickname: tiao.nickname,
    });
    let data1 = await find(message, {
      status: tiao.status,
    });
    let data2 = await find(message, {
      nickname: tiao.nickname,
      status: tiao.status,
    });
    if (tiao.nickname == "" && tiao.status != "") {
      res.send({
        code: 200,
        data: data1,
      });
    } else {
      if (tiao.nickname != "" && tiao.status == "") {
        res.send({
          code: 200,
          data: data,
        });
      } else {
        res.send({
          code: 200,
          data: data2,
        });
      }
    }
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

// 删除数据
const delCtr = async (req, res, next) => {
  try {
    let bob = await del(message, {
      id: req.body.id,
    });
    if (bob == 0) {
      res.send({
        code: 400,
        message: "删除失败，请联系程序员",
      });
      return;
    }
    res.send({
      code: 200,
      message: "删除成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "删除失败，请联系程序员",
    });
  }
};

//========================回收站=========================
//查询全部数据
const getRecycle = async (req, res, next) => {
  try {
    let data = await find(message1);
    res.send({
      code: 200,
      data,
    });
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

//新增
const postRecycle = async (req, res, next) => {
  let data = req.body;
  console.log(data);
  try {
    await add(message1, data);
    res.send({
      code: 200,
      message: "数据添加成功",
    });
  } catch (error) {
    res.send({
      code: 400,
      message: "数据添加失败",
    });
  }
};

//模糊查询
const getRecycles = async (req, res, next) => {
  let tiao = req.query;
  console.log(tiao);
  try {
    let data = await find(message1, {
      nickname: tiao.nickname,
    });
    let data1 = await find(message1, {
      status: tiao.status,
    });
    let data2 = await find(message1, {
      nickname: tiao.nickname,
      status: tiao.status,
    });
    if (tiao.nickname == "" && tiao.status != "") {
      res.send({
        code: 200,
        data: data1,
      });
    } else {
      if (tiao.nickname != "" && tiao.status == "") {
        res.send({
          code: 200,
          data: data,
        });
      } else {
        res.send({
          code: 200,
          data: data2,
        });
      }
    }
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

// 删除数据
const delRecycle = async (req, res, next) => {
  try {
    let bob = await del(message1, {
      id: req.body.id,
    });
    if (bob == 0) {
      res.send({
        code: 400,
        message: "删除失败，请联系程序员",
      });
      return;
    }
    res.send({
      code: 200,
      message: "删除成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "删除失败，请联系程序员",
    });
  }
};

// 恢复数据
const delRecycles = async (req, res, next) => {
  try {
    let bob = await del(message1, {
      id: req.body.id,
    });
    if (bob == 0) {
      res.send({
        code: 400,
        message: "恢复失败，请联系程序员",
      });
      return;
    }
    res.send({
      code: 200,
      message: "恢复成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "恢复失败，请联系程序员",
    });
  }
};
module.exports = {
  getCtr,
  postCtr,
  putCtr,
  getCtr1,
  delCtr,
  postCtr1,getRecycle,postRecycle,getRecycles,delRecycle,delRecycles
};
